using B.S.Data.MES.API.Services;
using B.S.Requst;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using WeatherInfo = B.S.Data.MES.API.Services.WeatherInfo;
using WeatherForecast = B.S.Data.MES.API.Services.WeatherForecast;
using Microsoft.AspNetCore.Authorization;

namespace B.S.Data.MES.API.Controllers
{
    /// <summary>
    /// 天气查询控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WeatherController : ControllerBase
    {
        private readonly IWeatherService _weatherService;
        private readonly ILogger<WeatherController> _logger;

        public WeatherController(IWeatherService weatherService, ILogger<WeatherController> logger)
        {
            _weatherService = weatherService;
            _logger = logger;
        }

        /// <summary>
        /// 根据地区名称查询天气信息
        /// </summary>
        /// <param name="regionName">地区名称</param>
        /// <returns>天气信息</returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<APIRequst<WeatherInfo>> GetWeatherByRegion([FromQuery] string regionName)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(regionName))
                {
                    return new APIRequst<WeatherInfo>
                    {
                        Code = RequstCode.失败,
                        Msg = "请输入地区名称",
                        Data = null
                    };
                }

                _logger.LogInformation("天气查询请求 - 地区: {Region}", regionName);
                var weatherInfo = await _weatherService.GetWeatherByRegionAsync(regionName);

                return new APIRequst<WeatherInfo>
                {
                    Code = RequstCode.成功,
                    Msg = "查询成功",
                    Data = weatherInfo
                };
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "天气查询异常: {Message}", ex.Message);
                return new APIRequst<WeatherInfo>
                {
                    Code = RequstCode.失败,
                    Msg = $"查询失败: {ex.Message}",
                    Data = null
                };
            }
        }

        ///// <summary>
        ///// 获取近七天天气预报
        ///// </summary>
        ///// <param name="regionName">地区名称</param>
        ///// <returns>近七天天气预报</returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<APIRequst<List<Services.WeatherForecast>>> Get7DayWeatherForecast([FromQuery] string regionName)
        {

                try
                {
                    if (string.IsNullOrWhiteSpace(regionName))
                    {
                        return new APIRequst<List<Services.WeatherForecast>>
                        {
                            Code = RequstCode.失败,
                            Msg = "请输入地区名称",
                            Data = null
                        };
                    }

                    _logger.LogInformation("近七天天气预报请求 - 地区: {Region}", regionName);

                    return new APIRequst<List<Services.WeatherForecast>>
                    {
                        Code = RequstCode.成功,
                        Msg = "查询成功",
                        Data = await _weatherService.Get7DayWeatherForecastAsync(regionName)
                    };
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "近七天天气预报查询异常: {Message}", ex.Message);
                    return new APIRequst<List<Services.WeatherForecast>>
                    {
                        Code = RequstCode.失败,
                        Msg = $"查询失败: {ex.Message}",
                        Data = null
                    };
                }
            

            
        }
    }
}